#include <REGX52.H>

//Function declaration
void UartInit(void);
void SendByte(unsigned char dat);
void SendString(unsigned char *dat);

void Interpolation_Init();
void Timer0Init();
void Interpolation();
//void delay(unsigned int xms);

//Global variable
unsigned char recv_Buff[5];
unsigned char recv_Flag = 0;
unsigned char noInit_Flag = 0;
unsigned char RXD_Num = 0;
unsigned char Xe=0, Ye=0;

char Interrupt_Num = 20;
unsigned char Nxy;
char Fm;
unsigned char send_str[5];

int main(void)
{
	UartInit();
	while(1)
	{
		if(recv_Flag == 1)
		{
			Interpolation_Init();
			recv_Flag = 0;
		}
	}
}

//Interrupt mode receive information 
//and Query mode send information
void UartInit(void)		//9600bps@11.0592MHz
{
	PCON &= 0x7F;		//波特率不倍速
	SCON = 0x50;		//8位数据,可变波特率	SM0 SM1 SM2 REN TB8 RB8 TI RI

	TMOD &= 0x0F;		//清除定时器1模式位
	TMOD |= 0x20;		//设定定时器1为8位自动重装方式

	TL1 = 0xFD;		//设定定时初值
	TH1 = 0xFD;		//设定定时器重装值
	ET1 = 0;		//禁止定时器1中断
	
	ES = 1;			//串口中断允许
	EA = 1;			//总中断允许
	TR1 = 1;		//启动定时器1	
}
//Query mode sends one byte
void SendByte(unsigned char dat)
{
	SBUF = dat;
	while(!TI);
	TI = 0;
}

//Query mode sends a string of data
void SendString(unsigned char *dat)
{
	while(*dat != '\0')
	{
		SendByte(*dat++);
	}
}
void Serial_ISR(void) interrupt 4
{
	if(RI)
	{
		RI = 0;
		
		recv_Buff[RXD_Num] = SBUF;
		RXD_Num++;
		if(RXD_Num>3)
		{
			if(((recv_Buff[0] == '+')|(recv_Buff[0] == '-')) \
				&&((recv_Buff[1] >= '1')&&(recv_Buff[1] <= '9')) \
					&&((recv_Buff[2] == '+')|(recv_Buff[2] == '-')) \
						&&((recv_Buff[3] >= '1')&&(recv_Buff[3] <= '9')))
			{
				RXD_Num = 0;
				Xe = recv_Buff[1] - 0x30;
				Ye = recv_Buff[3] - 0x30;
				if((recv_Flag == 0)&&(noInit_Flag == 0))
					recv_Flag = 1;
			}
		}
	}
}
//Timer0 Interrupt
void Timer0Init()
{
	TMOD &= 0xF0;
	TMOD |= 0x01;	//Use the Timer0 mode 1
	
	TH0 = 0x4C;
	TL0 = 0x00;		//Set Initial value, timing 50ms
	
	ET0 = 1;		//Timer0 Interrupt Allowed
	TR0 = 1;		//Start Timer0
}
void T0_ISR(void) interrupt 1
{
	TH0 = 0x4C;
	TL0 = 0x00;
	Interrupt_Num--;
	if(Interrupt_Num == 0)
	{
		Interrupt_Num = 20;
		//Perform interpolation once in 1s,
		//and send the result to the upper computer
		Interpolation();
	}
}
//Interpolation preprocessing
void Interpolation_Init()
{ 
	unsigned char i;
	Nxy = Xe + Ye;
	Fm = 0;
	
	for (i=0; i<sizeof(send_str)-1; i++) send_str[i] = 0;
	Timer0Init();
	noInit_Flag = 1;
}
//Interpolation function
void Interpolation()
{
	if(Fm >= 0) 
	{
		send_str[0] = '+';
		if(recv_Buff[0] == '+')
		{
			send_str[1] = '+';
			send_str[2] = 'x';
		}
		else
		{
			send_str[1] = '-';
			send_str[2] = 'x';
		}
				
		Fm = Fm - Ye;
		if(Fm >= 0)
		{
			send_str[3] = '+';
			send_str[4] = (Fm+0x30);
		}
		else
		{
			send_str[3] = '-';
			send_str[4] = (0-Fm+0x30);
		}
	}
	else
	{
		send_str[0] = '-';
		if(recv_Buff[2] == '+')
		{
			send_str[1] = '+';
			send_str[2] = 'y';
		}
		else
		{
			send_str[1] = '-';
			send_str[2] = 'y';
		}

		Fm = Fm + Xe;
		if(Fm >= 0)
		{
			send_str[3] = '+';
			send_str[4] = (Fm+0x30);
		}
		else
		{
			send_str[3] = '-';
			send_str[4] = (0-Fm+0x30);
		}
			
	}
	SendString(send_str);
	
	Nxy--;
	if(Nxy == 0) { TR0 = 0; noInit_Flag = 0;
//		delay(1000);
//		recv_Buff[4] = 'd';
//		SendString(recv_Buff);
	}
}
//void delay(unsigned int xms)
//{
//	unsigned int i,j;
//	for(i=xms; i>0; i--)
//		for(j=120; j>0; j--);
//}

